package com.sxt.sys.controller;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;













import com.sxt.sys.domain.User;
import com.sxt.sys.service.NewsService;
import com.sxt.sys.utils.DataGridView;
import com.sxt.sys.utils.WebUtils;
import com.sxt.sys.vo.NewsVo;


@Controller
@RequestMapping("news")
public class NewsController {
	      @Autowired
       private NewsService newsService;
		
		/**
		 * 跳转到客户管理的页面
		 */
	      //以下注释解决主要原因是Controller类中需要接收的是Date类型，
	      //但是在页面端传过来的是String类型，最终导致了这个错误
	   /*   @InitBinder
	      protected void init(HttpServletRequest request, ServletRequestDataBinder binder) {
	          SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	          dateFormat.setLenient(false);
	          binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
	      }*/
		@RequestMapping("toNewsManager")
		public String toNewsManager(){
			return "system/newsManager";
		}
		/**
		 * 查询
		 */
		@RequestMapping("loadAllNews")
		@ResponseBody
		public DataGridView loadAllNews(NewsVo newsVo){
			/*DataGridView dg = this.newsService.queryAllNews(newsVo);
			System.out.println(dg.getTotal());*/
			return  this.newsService.queryAllNews(newsVo);
		}
		@RequestMapping("addNews")
		@ResponseBody
		public Map<String,Object> addNews(NewsVo newsVo){
			User user = (User) WebUtils.getCurrentSession().getAttribute("user");
			newsVo.setCreatetime(new Date());
			newsVo.setOpername(user.getRealname());
			Map<String,Object> map=new HashMap<>();
			 try {
				 this.newsService.addNews(newsVo);
				 map.put("msg", "添加成功");
			} catch (Exception e) {
				e.printStackTrace();
				 map.put("msg", "添加失败");
			}
			return  map;
		}
		@RequestMapping("updateNews")
		@ResponseBody
		public Map<String,Object> updateNews(NewsVo newsVo){
			
			Map<String,Object> map=new HashMap<>();
			 try {
				 this.newsService.updateNews(newsVo);
				 map.put("msg", "修改成功");
			} catch (Exception e) {
				e.printStackTrace();
				 map.put("msg", "修改失败");
			}
			return  map;
		}
		
		//删除
		/*@RequestMapping("deleteNews")
		@ResponseBody
		public Map<String,Object> deleteNews(NewsVo newsVo){
			Map<String,Object> map=new HashMap<>();
			 try {
				 this.newsService.deleteNews(newsVo);
				 map.put("msg", "添加成功");
			} catch (Exception e) {
				e.printStackTrace();
				 map.put("msg", "添加失败");
			}
			return  map;
		}*/
		//删除
		@RequestMapping("deleteNews")
		@ResponseBody
		public void deleteNews(NewsVo newsVo){
			
				 this.newsService.deleteNews(newsVo);
		}
	}
